<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="客户类型" prop="customerType">

        <el-select v-model="queryParams.customerType" filterable placeholder="请选择">
          <el-option v-for="dict in dict.type.customer_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="queryParams.typeName" placeholder="请输入类型名称" clearable />
      </el-form-item>
      <el-form-item label="卡型号" prop="cardType">
        <el-select v-model="queryParams.cardType" placeholder="卡型号" clearable>
          <el-option key="" label="全部" value="" />
          <el-option key="102卡0" label="102卡" value="102卡" />
          <el-option key="1608卡" label="1608卡" value="1608卡" />
          <el-option key="4442卡" label="4442卡" value="4442卡" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['systemSetup:gasMeter:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['systemSetup:gasMeter:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gasMeterList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="类型编号" align="center" prop="entId" /> -->
      <el-table-column label="类型名称" align="center" width="180" prop="typeName" />
      <el-table-column label="生产厂家" align="center" prop="manufacturerName" />
      <el-table-column label="卡型号" align="center" prop="cardType" />
      <el-table-column label="接入协议" align="center" width="180" prop="accessProtocol" />
      <!-- <el-table-column label="表计类型" align="center" prop="description" /> -->
      <el-table-column label="通讯方式" align="center" prop="communicateMode" />
      <el-table-column label="最大气量" align="center" prop="maxGas" />
      <el-table-column label="计量类型" align="center" prop="meteringType">
        <template slot-scope="scope">
          <span v-if="scope.row.meteringType == 0">气量</span>
          <span v-if="scope.row.meteringType == 1">余额</span>
        </template>
      </el-table-column>
      <el-table-column label="开户卡费" align="center" prop="openCardFee" />
      <el-table-column label="指令格式" align="center" prop="orderFormat" />
      <el-table-column label="补卡费用" align="center" prop="reissueCardFee" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">启用</span>
          <span v-if="scope.row.status == 2">停用</span>
        </template>
      </el-table-column>

      <el-table-column label="支持卡" align="center" prop="supportCard">
        <template slot-scope="scope">
          <span v-if="scope.row.supportCard == 0">不支持</span>
          <span v-if="scope.row.supportCard == 1">IC卡</span>
          <span v-if="scope.row.supportCard == 2">NFC卡</span>
        </template>
      </el-table-column>
      <el-table-column label="支持网络" align="center" prop="supportNet" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.supportNet == 0">不支持</span>
          <span v-if="scope.row.supportNet == 1">支持</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="客户类型" align="center" prop="customerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_type" :value="scope.row.customerType" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['systemSetup:gasMeter:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['systemSetup:gasMeter:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="12">
            <el-form-item label="客户类型" prop="customerType">
              <el-select v-model="form.customerType" filterable placeholder="请选择" style='width:100%'>
                <el-option v-for="dict in dict.type.customer_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="支持网络" prop="supportNet">
              <el-select v-model="form.supportNet" placeholder="支持网络" clearable style='width:100%'>
                <el-option :key="0" label="不支持" :value="0" />
                <el-option :key="1" label="支持" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支持卡" prop="supportCard">
              <el-select v-model="form.supportCard" placeholder="支持卡" clearable style='width:100%'>
                <el-option :key="0" label="不支持" :value="0" />
                <el-option :key="1" label="支持IC卡" :value="1" />
                <el-option :key="2" label="NFC卡" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡型号" prop="cardType">
              <el-select v-model="form.cardType" placeholder="卡型号" clearable style='width:100%'>
                <el-option key="102卡0" label="102卡" value="102卡" />
                <el-option key="1608卡" label="1608卡" value="1608卡" />
                <el-option key="4442卡" label="4442卡" value="4442卡" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计量类型" prop="meteringType">
              <el-select v-model="form.meteringType" placeholder="最大气量" clearable style='width:100%'>
                <el-option :key="0" label="气量" :value="0" />
                <el-option :key="1" label="余额" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入类型名称" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="所属类型" prop="type">
              <el-select v-model="form.type" placeholder="所属类型" clearable style='width:100%'>
                <el-option key="1" label="1" value="1" />
                <el-option key="2" label="2" value="2" />
                <el-option key="3" label="3" value="3" />
              </el-select>
            </el-form-item>
          </el-col> -->

          <el-col :span="12">
            <el-form-item label="生产厂家" prop="manufacturerId">
              <el-select v-model="form.manufacturerId" filterable placeholder="请选择" style='width:100%'>
                <el-option :label="item.supplierName" :value="item.id" v-for="(item, index) in supplierList" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="卡型号" prop="cardType">
              <el-input v-model="form.cardType" placeholder="请输入卡型号" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="接入协议" prop="accessProtocol">
              <el-input v-model="form.accessProtocol" placeholder="请输入接入协议" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通讯方式" prop="communicateMode">
              <el-input v-model="form.communicateMode" placeholder="请输入通讯方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大气量" prop="maxGas">
              <el-input v-model="form.maxGas" placeholder="请输入最大气量" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户卡费" prop="openCardFee">
              <el-input v-model="form.openCardFee" placeholder="请输入开户卡费" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指令格式" prop="orderFormat">
              <el-input v-model="form.orderFormat" placeholder="请输入指令格式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="补卡费用" prop="reissueCardFee">
              <el-input v-model="form.reissueCardFee" placeholder="请输入补卡费用" type="number" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="报废标志" clearable style='width:100%'>
                <el-option key="1" label="启用" value="1" />
                <el-option key="2" label="停用" value="2" />
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGasmeter,
  getGasmeter,
  addGasmeter,
  updateGasmeter,
  delGasmeter,
} from "@/api/systemSetup/gasMeter";
import { listSupplier, } from "@/api/system/supplierInfo.js";

export default {
  name: "gasMeter",
  dicts: ["customer_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 燃气表表格数据
      gasMeterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        categoryName: undefined,
        cardType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryName: [
          { required: true, message: "类型名称不能为空", trigger: "blur" },
        ],
        supportCard: [
          { required: true, message: "支持卡不能为空", trigger: "change" },
        ],
        supportNet: [
          { required: true, message: "支持网络不能为空", trigger: "change" },
        ],
        customerType: [
          { required: true, message: "客户类型不能为空", trigger: "change" },
        ],
        meteringType: [
          { required: true, message: "计量类型不能为空", trigger: "change" },
        ],
        manufacturerId: [
          { required: true, message: "生产厂家不能为空", trigger: "change" },
        ],
        typeName: [
          { required: true, message: "类型名称不能为空", trigger: "change" },
        ]

      },
      supplierList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商列表 */
    getsupplierList() {
      let params = {
        pageNum: 1,
        pageSize: 9999,

      }
      listSupplier(params).then((res) => {
        if (res.code === 200) {
          this.supplierList = res.rows;
        } else {
          this.$message.error(res.msg);
        }


      });
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listGasmeter(this.queryParams).then((res) => {
        if (res.code === 200) {
          this.gasMeterList = res.rows;
          this.total = res.total;
          this.loading = false;
        } else {
          this.$message.error(res.msg);
          this.loading = false;
        }

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        categoryNumber: undefined,
        categoryName: undefined,
        manufacturer: undefined,
        cardType: undefined,
        type: undefined,
        alarmGas: undefined,
        safetySigns: undefined,
        safetyPeriod: undefined,
        scrapSigns: undefined,
        scrapPeriod: undefined,
        limitAmount: undefined,
        overdraftGas: undefined,
        isWechatPay: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.categoryNumber);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {
        supportNet: 1,
        customerType: '1',
        meteringType: 1,
        status: '1',
        supportCard: 0,
        cardType: '',
      },
        this.open = true;
      this.title = "气表类型新增";
      this.getsupplierList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.id;
      getGasmeter(postId).then((res) => {
        if (res.code === 200) {
          this.form = res.data;
          this.open = true;
          this.title = "气表类型修改燃";
        } else {
          this.$message.error(res.msg);
        }

      });
      this.getsupplierList()
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGasmeter(this.form).then((res) => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(res.msg);
              }

            });
          } else {
            addGasmeter(this.form).then((res) => {
              if (res.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(res.msg);
              }

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id;
      this.$modal
        .confirm("是否确认删除该条数据？")
        .then(function () {
          return delGasmeter(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
<style>
.el-dialog__title {
  font-size: 18px;
}
</style>